<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>论文浏览</title>
    <link rel="stylesheet"  href="../static/layui/css/layui.css" >
    <style>
        body{margin: 10px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
</head>
<body>
<table class="layui-hide"  id="demo" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-hide" lay-event="del" >删除</a>
</script>

<div class="layui-tab layui-tab-brief" lay-filter="demo">
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
        </div>
        <div class="layui-tab-item">
            <div id="pageDemo"></div>
        </div>
    </div>
</div>
<script src="../static/layui/js/jquery.min.js"></script>
<script src="../static/layui/layui.js"></script>
<script>

    layui.use([ 'laypage',  'table'], function(){
        var laypage = layui.laypage //分页
            ,table = layui.table //表格
            $ = layui.jquery;

        //执行一个 table 实例

        table.render({
            elem: '#demo'
            ,height: 450
            ,url: 'http://localhost:8090/paper/list' //数据接口
            ,method: 'post'
            ,request: {
                pageName: 'curr' //页码的参数名称，默认：page
                ,limitName: 'nums' //每页数据量的参数名，默认：limit
             }
            ,paraDate: function (res) { //res 原始返回数据
                return{
                    "msg": res.status
                }
            }
            ,title: '论文'
            ,page: true //开启分页
            ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'pId', title: 'ID', width:80, fixed: 'left' ,sort:true}
                ,{field: 'pName', title: '论文名', width:100}
                ,{field: 'pKeywords', title: '关键词', width: 150}
                ,{field: 'pJournal', title: '期刊', width:150}
                ,{field: 'pDate', title: '日期', width:200,sort:true}
                ,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemo'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.msg('添加');
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        layer.alert('编辑 [id]：'+ checkStatus.data[0].id);
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        layer.msg('删除');
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,pFilepath = obj.data.pFilepath
                ,pId = obj.data.pId //获得论文id
                ,pKeywords=obj.data.pKeywords//获得关键词
                ,uId = 1    //用户登录未完成，暂时1代替
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
                //brower
                $.ajax({
                    type:"POST",
                    contenttype:"application/x-www-form-urlencoded",
                    url:"http://localhost:8090/hotword/browser",
                    data:{
                        uId:uId,
                        pId :pId
                    },
                    xhrFields:{withCredentials:true},
                    success:function (data) {
                        if(data.status == "success"){
                            alert("成功绑定")
                        }else{
                            alert("未成功 原因为"+data.status.errMsg)
                        }
                    },
                    error:function (data) {
                        alert("失败"+data.responseText)
                    }
                });


                //用户查看，从路径读取论文跳转
                console.log(obj.data.pFilepath);
                window.location.href="readPdf.html?pFilepath="+pFilepath;

            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){
                layer.msg('编辑操作');
            }
        });
        //分页
        laypage.render({
            elem: 'pageDemo' //分页容器的id
            ,count: 40//总页数
             ,limit:3
             ,limits:[2,3,5]
            ,skin: '#1E9FFF' //自定义选中色值
            //,skip: true //开启跳页
            ,jump: function(obj, first){
                if(!first){
                    layer.msg('第'+ obj.curr +'页', {offset: 'b'});
                }
            }
        });
    });
</script>
</body>
</html>
